package com.gaohe;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gaohe.pojo.User;
import com.gaohe.service.UserService;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SpringBootTest(classes = Application.class)
public class MpTest03 {
    @Autowired
    private UserService service;

    /*
        1.按条件查询
        2.链式编程
        3.lambda 表达式
        4.lambda 表达式
     */
    // 18<age < 35
//    1.按条件查询
    @Test
    public void selectByConditions01() {
        //
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lt("age", 35);
        qw.gt("age", 18);
        List<User> list = service.list(qw);
        System.out.println(list);
    }

    // 2.链式编程
    @Test
    public void selectByConditions02() {
        //
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lt("age", 35)
                .gt("age", 18);
        List<User> list = service.list(qw);
        System.out.println(list);
    }

    // 3.lambda 表达式
    @Test
    public void selectByConditions03() {
        //
        QueryWrapper<User> qw = new QueryWrapper<>();
        //构造引用  类名::方法名  -> 获取使用的属性名
        qw.lambda().lt(User::getAge, 35)
                .gt(User::getAge, 18);
        List<User> list = service.list(qw);
        System.out.println(list);
    }

    // 4.lambda 表达式
    @Test
    public void selectByConditions04() {
        //
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        //构造引用  类名::方法名  -> 获取使用的属性名
        qw.lt(User::getAge, 35)
                .gt(User::getAge, 18);
        List<User> list = service.list(qw);
        System.out.println(list);
    }

    //  aeg < 18  or age >35
    @Test
    public void selectByConditions05() {
        //
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        //构造引用  类名::方法名  -> 获取使用的属性名
        qw.lt(User::getAge, 18)
                .or().gt(User::getAge, 35);

        List<User> list = service.list(qw);
        System.out.println(list);
    }

    //18<=age <= 35
    @Test
    public void selectByConditions06() {
        //
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        //构造引用  类名::方法名  -> 获取使用的属性名
//       qw.le(User::getAge,35)
//               .ge(User::getAge,18);
        qw.between(User::getAge, 18, 35);
        List<User> list = service.list(qw);
        System.out.println(list);


    }

    //多条件查询
    @Test
    public void selectByConditions07() {
        Integer ageMin = 18;
        Integer ageMax = null;
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        //构造引用  类名::方法名  -> 获取使用的属性名
//        if (ageMin !=null){
//            qw.ge(User::getAge,ageMin);
//        }
//        if (ageMax !=null){
//            qw.le(User::getAge,ageMax);
//        }
        qw.le(ageMax != null, User::getAge, ageMax)
                .ge(ageMin != null, User::getAge, ageMin);

        List<User> list = service.list(qw);
        System.out.println(list);
    }

    //多条件查询
    @Test
    public void selectByConditions08() {
        Integer ageMin = 18;
        Integer ageMax = null;
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.select("count(*),age");
        qw.groupBy("age");

//        List<User> list = service.list(qw);
        List<Map<String, Object>> maps = service.listMaps(qw);
        System.out.println(maps);
    }

    //2.1 查询结果包含模型类中部分属性
    @Test
    public void selectByConditions09() {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.select(User::getName, User::getAge);
//        List<User> list = service.list(qw);
        List<User> list = service.list(qw);
        System.out.println(list);
    }

    //属性名 字段名 不匹配问题
    @Test
    public void selectByConditions10() {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
//        qw.select(User::getName, User::getAge);
//        List<User> list = service.list(qw);
        List<User> list = service.list(qw);
        System.out.println(list);
    }

    //属性中  数据库字段不存在
    @Test
    public void selectByConditions11() {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
//        qw.select(User::getName, User::getAge);
//        List<User> list = service.list(qw);
        List<User> list = service.list();
        System.out.println(list);
    }

    //精准匹配
    @Test
    public void selectByConditions12() {
        String name = "jiang";
        String password = "kai";
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getName, name)
                .eq(User::getPass, password);
        User user = service.getOne(qw);
        System.out.println(user);
    }

    //模糊查询
    @Test
    public void selectByConditions13() {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        //1.姓张的  张%
//        qw.likeRight(User::getName,"张");
        //2.姓名中包含 炮
//        qw.like(User::getName,"炮");
        //3. 已 xx达 结尾
        qw.likeLeft(User::getName, "达");
        List<User> list = service.list(qw);
        System.out.println(list);
    }

    //批量操作
    @Test
    public void selectByConditions14() {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
//        //1.批量删除
//        List<Long> ids = new ArrayList<>();
//        ids.add(990380033l);
//        ids.add(-1018691583l);
//        service.removeByIds(ids);
        //批量新增
        List<User> users = new ArrayList<>();
        User user1 = new User();
        user1.setName("jiangxiang");
        user1.setPass("kai");
        user1.setAge(18);
        user1.setTel("22222222222");
        user1.setClassName("一班");
        User user2 = new User();
        user2.setName("jiangxiang");
        user2.setPass("kai");
        user2.setAge(18);
        user2.setTel("22222222222");
        user2.setClassName("一班");
        users.add(user1);
        users.add(user2);
        boolean saveBatch = service.saveBatch(users);
    }
    //逻辑删除
    @Test
    public void deleteById(){
        service.removeById(1971862136997568513l);
        System.out.println(service.list());
    }

    //乐观锁  锁  多线程  解决线程安全问题
    //有共享变量  写  （修改）
    //秒杀  1--0 -1   超卖
    // >0     --
    // >0 --
    @Test
    public void optimismLock(){
//        service.removeById(1971862136997568513l);
//        System.out.println(service.list());
//        User user = new User();
//        user.setId(1l);
//        user.setName("zs");
//        //设置 version
//        user.setVersion(0);
//        service.updateById(user);
        // 修改之前  先 查询
        User user1 = service.getById(1l);
        User user2 = service.getById(1l);
        user1.setName("user111");
        user2.setName("user2222");
        service.updateById(user1);
        service.updateById(user2);
    }
}
